package com.gs.SynPatrolRep.control;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gs.SynPatrolRep.R;
import com.gs.SynPatrolRep.adapter.TooBarListAdapter;
import com.gs.SynPatrolRep.adapter.base.BaseAdapter;
import com.gs.SynPatrolRep.db.dto.ShortDto;

import java.util.List;

public class ToolBarOption extends LinearLayout {
    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 10001:
                    adapter.notifyDataSetChanged();
                    if (onOptionItemClickListener != null) {
                        onOptionItemClickListener.OnOptionItemClick(null, msg.obj);
                    }
                    break;
            }
        }
    };

    public interface OnOptionItemClickListener {
        void OnOptionItemClick(View v, Object option);
    }

    private OnOptionItemClickListener onOptionItemClickListener;

    public void setOnOptionItemClickListener(OnOptionItemClickListener onOptionItemClickListener) {
        this.onOptionItemClickListener = onOptionItemClickListener;
    }

    private RecyclerView item_recyclerview;
    private List<ShortDto> list;
    private TooBarListAdapter adapter;
    private Context context;

    public void setList(final List<ShortDto> list) {
        if (list == null) return;
        this.list = list;
        list.add(0, new ShortDto("", context.getString(R.string.all), true));
        adapter = new TooBarListAdapter(context, list, R.layout.tool_bar_option_item);
        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View v, final int position) {
                changeState(position);
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        for (int i = 0; i <= list.size() - 1; i++) {
//                            list.get(i).setSelect(false);
//                        }
//                        list.get(position).setSelect(true);
//                        Message msg = Message.obtain();
//                        msg.obj = list.get(position);
//                        msg.what = 10001;
//                        handler.sendMessage(msg);
//                    }
//                }).start();

            }
        });
        item_recyclerview.setAdapter(adapter);
    }

    public void changeState(int position){
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= list.size() - 1; i++) {
                    list.get(i).setSelect(false);
                }
                list.get(position).setSelect(true);
                Message msg = Message.obtain();
                msg.obj = list.get(position);
                msg.what = 10001;
                handler.sendMessage(msg);
            }
        }).start();
    }

    public ToolBarOption(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
//        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.TooBarOption);
        LayoutInflater.from(context).inflate(R.layout.tool_bar_option, this, true);
        item_recyclerview = findViewById(R.id.item_recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        item_recyclerview.setLayoutManager(layoutManager);
//        attributes.recycle();
    }
}